import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../controller/home_controller.dart';

class SecondPage extends StatelessWidget {
  const SecondPage({super.key});

  @override
  Widget build(BuildContext context) {
    final HomeController controller = Get.find<HomeController>();
    final args = Get.arguments;
    final userId = args?['userId'];
    final isAdmin = args?['isAdmin'];
    return Scaffold(
      appBar: AppBar(
        title: const Text('第二页'),
        leading: IconButton(
          icon: const Icon(Icons.home),
          onPressed: () => Get.offAllNamed('/'),
        ),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            if (args != null)
              Padding(
                padding: const EdgeInsets.only(bottom: 12),
                child: Text(
                  'userId: $userId, isAdmin: $isAdmin',
                  style: const TextStyle(fontSize: 18, color: Colors.blue),
                ),
              ),
            Obx(() => Text(
                '姓名：${controller.user.value.name}, 年龄：${controller.user.value.age}',
                style: const TextStyle(fontSize: 24))),
            const SizedBox(height: 20),
            ElevatedButton(
              onPressed: () => controller.updateUserName('SecondPageName'),
              child: const Text('改名'),
            ),
            const SizedBox(height: 20),
            ElevatedButton(
              onPressed: controller.incrementAge,
              child: const Text('年龄+1'),
            ),
            const SizedBox(height: 20),
            ElevatedButton(
              onPressed: () =>
                  Get.back(result: {'fromSecond': true, 'msg': 'SecondPage回传'}),
              child: const Text('返回首页'),
            ),
          ],
        ),
      ),
    );
  }
}
